<template>
  <div class="dashboard-container">
    <div class="dashboard-header">
      <h1>仪表板</h1>
      <p>欢迎回来，{{ user?.username }}！</p>
    </div>

    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon primary">👤</div>
        <div class="stat-content">
          <h3>个人信息</h3>
          <p class="stat-number">已完善</p>
          <p class="stat-desc">查看和编辑您的个人信息</p>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon success">📊</div>
        <div class="stat-content">
          <h3>账户状态</h3>
          
          <p class="stat-desc">活跃</p>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon warning">📅</div>
        <div class="stat-content">
          <h3>注册时间</h3>
         
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon info">🔄</div>
        <div class="stat-content">
          <h3>最后更新</h3>

        </div>
      </div>
    </div>

    <div class="dashboard-content">
      <div class="content-grid">
        <div class="quick-actions-card">
          <h3>快速操作</h3>
          <div class="actions-list">
            <router-link to="/profile" class="action-item">
              <span class="action-icon">👤</span>
              <div class="action-content">
                <h4>查看个人信息</h4>
                <p>查看和编辑您的个人资料</p>
              </div>
              <span class="action-arrow">→</span>
            </router-link>
            
            <router-link to="/admin/users" class="action-item" v-if="isAdmin">
              <span class="action-icon">⚙️</span>
              <div class="action-content">
                <h4>用户管理</h4>
                <p>管理系统中的所有用户</p>
              </div>
              <span class="action-arrow">→</span>
            </router-link>
            
            <div class="action-item" @click="handleLogout">
              <span class="action-icon">🚪</span>
              <div class="action-content">
                <h4>退出登录</h4>
                <p>安全退出系统</p>
              </div>
              <span class="action-arrow">→</span>
            </div>
          </div>
        </div>

        <div class="user-info-card">
          <h3>个人信息概览</h3>
          <div class="user-details">
            <div class="detail-item">
              <label>用户名:</label>
              <span>{{ user?.username }}</span>
            </div>
            <div class="detail-item">
              <label>邮箱:</label>
              <span>{{ user?.email }}</span>
            </div>
            
            <div class="detail-item">
              <label>用户角色:</label>
              <span :class="userRoleClass">{{ userRoleText }}</span>
            </div>
            <div class="detail-item">
              <label>账户状态:</label>
              <label>活跃</label>
              
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import type { User } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()

const user = ref<User | null>(null)

const isAdmin = computed(() => userStore.userRole === 'admin')

const userRoleText = computed(() => {
  return userStore.userRole === 'admin' ? '管理员' : '普通用户'
})

const userRoleClass = computed(() => {
  return userStore.userRole === 'admin' ? 'role-admin' : 'role-user'
})

const formatDate = (dateString?: string) => {
  if (!dateString) return '-'
  return new Date(dateString).toLocaleDateString('zh-CN')
}

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

const loadUser = async () => {
  const userInfo = localStorage.getItem('userInfo')
  if (userInfo) {
    user.value = JSON.parse(userInfo)
  } else {
    const userData = await userStore.fetchCurrentUser()
    if (userData) {
      user.value = userData
    }
  }
}

onMounted(() => {
  loadUser()
})
</script>

<style scoped>
.dashboard-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.dashboard-header {
  margin-bottom: 40px;
}

.dashboard-header h1 {
  font-size: 36px;
  color: #1e293b;
  margin-bottom: 8px;
  font-weight: 600;
}

.dashboard-header p {
  color: #64748b;
  font-size: 18px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 24px;
  margin-bottom: 40px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  border: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  gap: 20px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.stat-icon.primary {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: white;
}

.stat-icon.success {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
}

.stat-icon.warning {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: white;
}

.stat-icon.info {
  background: linear-gradient(135deg, #6b7280, #4b5563);
  color: white;
}

.stat-content h3 {
  font-size: 16px;
  color: #64748b;
  margin-bottom: 8px;
  font-weight: 500;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 4px;
}

.stat-desc {
  font-size: 14px;
  color: #94a3b8;
}

.status-active {
  color: #10b981;
}

.status-inactive {
  color: #ef4444;
}

.dashboard-content {
  margin-top: 40px;
}

.content-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

.quick-actions-card,
.user-info-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  border: 1px solid #e2e8f0;
  padding: 30px;
}

.quick-actions-card h3,
.user-info-card h3 {
  font-size: 20px;
  color: #1e293b;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f1f5f9;
}

.actions-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 12px;
  border: 1px solid #f1f5f9;
  transition: all 0.2s;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}

.action-item:hover {
  background: #f8fafc;
  border-color: #e2e8f0;
  transform: translateX(4px);
}

.action-icon {
  font-size: 20px;
  width: 40px;
  height: 40px;
  background: #f1f5f9;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-content {
  flex: 1;
}

.action-content h4 {
  font-size: 16px;
  color: #1e293b;
  margin-bottom: 4px;
  font-weight: 600;
}

.action-content p {
  font-size: 14px;
  color: #64748b;
}

.action-arrow {
  color: #94a3b8;
  font-size: 18px;
  font-weight: bold;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f8fafc;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-item label {
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.detail-item span {
  color: #64748b;
  font-size: 14px;
}

.role-admin {
  color: #3b82f6;
  font-weight: 500;
  background: #dbeafe;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
}

.role-user {
  color: #6b7280;
  font-weight: 500;
  background: #f3f4f6;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-grid {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .stat-card {
    flex-direction: column;
    text-align: center;
    gap: 16px;
  }
  
  .detail-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .detail-item span {
    text-align: left;
  }
}
</style>